package com.fhnew.app.model.internet;

import android.util.Base64;
import android.util.Log;

import com.fhnew.app.BuildConfig;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.internet.been.B_AllThirdList;
import com.fhnew.app.model.internet.been.B_EconomyCount;
import com.fhnew.app.model.internet.been.B_InTuE01;
import com.fhnew.app.model.internet.been.B_CommonGames;
import com.fhnew.app.model.internet.been.B_GameOver;
import com.fhnew.app.model.internet.been.B_GameWebUrl;
import com.fhnew.app.model.internet.been.B_HotList;
import com.fhnew.app.model.internet.been.B_Login;
import com.fhnew.app.model.internet.been.B_OutYuE1;
import com.fhnew.app.model.internet.been.B_RateList;
import com.fhnew.app.model.internet.been.B_RootList;
import com.fhnew.app.model.internet.been.B_SLGames;
import com.fhnew.app.model.internet.been.B_ThirdMoneyList;
import com.fhnew.app.model.internet.been.B_UserMoney;
import com.fhnew.app.model.internet.been.B_YuEDetail;
import com.fhnew.app.model.internet.been.B_YuEMain;
import com.fhnew.app.model.internet.been.Been;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.util.HLog;


import java.util.ArrayList;
import java.util.List;

import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;

/**
 * 数据请求与解析的封装
 * <p>
 * <p>
 * （1）用户相关接口
 * 1、请求服务器地址：{@link GetRootUrl}
 * 2、登录接口： {@link PostLogin}
 * 3、获取用户金币 {@link }
 * <p>
 * （2）游戏列表相关接口
 * <p>
 * 3、获取热门游戏列表：{@link GetHotList}
 * 4、获取其他游戏列表： {@link GetCommonGames}
 * 5、获取Web加载的游戏Url {@link GetGameUrl}
 * 6、获取二级游戏列表  {@link GetGameSecondLeveList}
 * <p>
 * <p>
 * （3）三方游戏相关
 * 1）获取三方供应商列表   {@link GetAllThirdMoneyList}
 * 2）下分接口  {@link GetThirdGameMoney}
 * 3）获取当个平台分数 {@link GetRefreshThirdGameMoney}
 *
 * <p>
 * 3、获取金币： {@link }
 */
public class HttpAPI {

    // https://kwfygyd.ws88168.com
    public static abstract class GetRootUrl extends HttpRequest<B_RootList> {

        public void startRequest () {
            Parameter parameter = new Parameter();
            parameter.setUrl(BuildConfig.ROOT_URL);
            parameter.methodGet();
            startRequest(parameter);
        }

        @Override
        public B_RootList decodeResult (String json) throws Exception {
            B_RootList beenHost = gson.fromJson(json, B_RootList.class);

            List<String> abc = new ArrayList<>();
            List<String> hosts = new ArrayList<>();
            for (int i = 0; i < beenHost.getAbc().size(); i++) {
                String temp = new String(Base64.decode(beenHost.getAbc().get(i).getBytes(), Base64.DEFAULT));
                String def = new String(Base64.decode((((String) beenHost.getDef().get(i))).getBytes(), Base64.DEFAULT));
                temp = java.net.URLDecoder.decode(temp, "utf-8");
                if (temp.contains("/m/#/home")) {
                    int index = temp.indexOf("/m/#/home/?android=true");
                    hosts.add(temp.substring(0, index));
                } else {
                    hosts.add(temp);
                }
                Constants.versionurl = java.net.URLDecoder.decode(def, "utf-8");
                Constants.versionName = beenHost.getVerson() + "";
                Constants.content = beenHost.getVerson() + "";
            }
            beenHost.setAbc(abc);
            beenHost.setHosts(hosts);
            List<String> def = new ArrayList<>();
            for (int i = 0; i < beenHost.getDef().size(); i++) {
                String temp = new String(Base64.decode(beenHost.getDef().get(i).getBytes(), Base64.DEFAULT));
                temp = java.net.URLDecoder.decode(temp, "utf-8");
                def.add(temp);
            }
            beenHost.setDef(def);
            return beenHost;
        }
    }

    public static abstract class PostLogin extends HttpRequest<B_Login> {

        public void startRequest (String userName, String userPassword) {
            Parameter parameter = new Parameter();
            parameter.methodPost();
            parameter.setUrl(Constants.adress + "/api/m/login?source=android");
            parameter.addValue("username", userName);
            parameter.addValue("password", userPassword);
            startRequest(parameter);
        }

        @Override
        public B_Login decodeResult (String json) throws Exception {
            B_Login rootList = gson.fromJson(json, B_Login.class);
            return rootList;
        }

    }

    public static abstract class GetUserMoney extends HttpRequest<B_UserMoney> {


        public void startRequest (String token) {
            Parameter parameter = new Parameter();
            parameter.methodPost();
            parameter.setUrl(Constants.adress + "/api/m/user/getMoney?source=android");
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_UserMoney decodeResult (String json) throws Exception {
            B_UserMoney userMoney = gson.fromJson(json, B_UserMoney.class);
            return (B_UserMoney) UserModel.loginStatusCheck(userMoney);
        }

    }

    public abstract static class GetHotList extends HttpRequest<B_HotList> {

        public void startRequest () {
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/GamesList/hotListPlay?source=android");
            startRequest(parameter);
        }

        @Override
        public B_HotList decodeResult (String json) throws Exception {
            B_HotList bHotList = gson.fromJson(json, B_HotList.class);
            return bHotList;
        }
    }

    public static abstract class GetCommonGames extends HttpRequest<B_CommonGames> {
        public void startRequest () {
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/m/lottery/getHomeLotteryDataNew?source=android");
            startRequest(parameter);
        }

        @Override
        public B_CommonGames decodeResult (String json) throws Exception {
            B_CommonGames commonGames = gson.fromJson(json, B_CommonGames.class);
            return commonGames;
        }
    }

    public abstract static class GetGameUrl extends HttpRequest<B_GameWebUrl> {
        private int game_id;

        public void startRequest (int game_id, String tag, String token, String doingCode) {
            this.game_id = game_id;
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/action?source=android");
            parameter.addValue("doingCode", doingCode);//获取游戏地址
            parameter.addValue("game_id", game_id);
            parameter.addValue("tag", tag);
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_GameWebUrl decodeResult (String json) throws Exception {
            B_GameWebUrl comeToGame = gson.fromJson(json, B_GameWebUrl.class);
            comeToGame.getData().setGame_id(game_id);
            return (B_GameWebUrl) UserModel.loginStatusCheck(comeToGame);
        }
    }

    //获取游戏列表
    public abstract static class GetGameSecondLeveList extends HttpRequest<B_SLGames> {

        private int game_id = 0;
        private boolean requesting = false;

        public void startRequest (int game_id, String token, int index, String doingCode) {
            if (requesting) {
                return;
            }
            requesting = true;
            this.game_id = game_id;
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/action?source=android");
            parameter.addValue("doingCode", doingCode);//获取二级列表
            parameter.addValue("game_id", game_id);
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_SLGames decodeResult (String json) throws Exception {
            requesting = false;
            B_SLGames slGames = gson.fromJson(json, B_SLGames.class);
            slGames.setGameId(game_id);
            return (B_SLGames) UserModel.loginStatusCheck(slGames);
        }
    }


    public abstract static class GetAllThirdMoneyList extends HttpRequest<B_ThirdMoneyList> {

        public void startRequest (String token) {
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/allMoney?source=android");
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_ThirdMoneyList decodeResult (String json) throws Exception {
            B_ThirdMoneyList thirdGameList = gson.fromJson(json, B_ThirdMoneyList.class);
            return (B_ThirdMoneyList) UserModel.loginStatusCheck(thirdGameList);
        }

    }


    //获取三方游戏列表
    public abstract static class GetThirdGameList extends HttpRequest<B_AllThirdList> {

        public void startRequest (String token) {
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/list_2?source=android");
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_AllThirdList decodeResult (String json) throws Exception {
            B_AllThirdList allThirdList = gson.fromJson(json, B_AllThirdList.class);
            return (B_AllThirdList) UserModel.loginStatusCheck(allThirdList);
        }

    }


    //获取三方平台游戏金额
    public abstract static class GetThirdGameMoney extends HttpRequest<B_GameOver> {

        private int game_id;

        public void startRequest (int game_id, String token) {
            this.game_id = game_id;
            Parameter parameter = new Parameter();
            parameter.methodGet();
            parameter.setUrl(Constants.adress + "/api/GamesApi/allDown?source=android");
            parameter.addValue("token", token);
            parameter.addValue("game_id", game_id);
            parameter.addValue("doingCode", "1");
            startRequest(parameter);
        }

        @Override
        public B_GameOver decodeResult (String json) throws Exception {
            B_GameOver been = gson.fromJson(json, B_GameOver.class);
            been.getData().setGame_id(game_id);
            return (B_GameOver) UserModel.loginStatusCheck(been);
        }
    }

    public abstract static class GetRefreshThirdGameMoney extends HttpRequest<B_GameOver> {

        private int game_id;

        public void startRequest (int game_id, String token) {
            this.game_id = game_id;
            Parameter parameter = new Parameter();
            parameter.methodGet();
            //https://www.ws88168.com/api/GamesApi/action?doingCode=1&game_id=10&token=8QVInoYMDtQs6Aa3SmKSlUUQHDnv3o3NksJh8yKK&source=pc
            parameter.setUrl(Constants.adress + "/api/GamesApi/action?source=android");
            parameter.addValue("doingCode", "1");//获取二级列表
            parameter.addValue("game_id", game_id);
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_GameOver decodeResult (String json) throws Exception {
            B_GameOver b_gameOver = gson.fromJson(json, B_GameOver.class);
            b_gameOver.getData().setGame_id(game_id);
            return (B_GameOver) UserModel.loginStatusCheck(b_gameOver);
        }
    }


    //=====================================================================
    // 余额宝相关接口
    //=====================================================================
    //余额宝首页
    public abstract static class GetYuE extends HttpRequest<B_YuEMain> {

        public void startRequest (String token) {
            Parameter parameter = new Parameter();
            parameter.methodPost();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/home?source=android");
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_YuEMain decodeResult (String json) throws Exception {
            B_YuEMain b_yuEMain = gson.fromJson(json, B_YuEMain.class);
            return (B_YuEMain) UserModel.loginStatusCheck(b_yuEMain);
        }
    }

    //转入余额宝（1）
    public abstract static class GetInYuE extends HttpRequest<B_InTuE01> {

        public void startRequest (String token, int gameId, int money, String fundPwd) {
            Parameter parameter = new Parameter();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/transferinP?source=android");
            parameter.methodPost();
            parameter.addValue("token", token);
            parameter.addValue("gameId", gameId);
            parameter.addValue("money", money);
            parameter.addValue("fundPwd", fundPwd);
            startRequest(parameter);
        }

        @Override
        public B_InTuE01 decodeResult (String json) throws Exception {
            B_InTuE01 chargeToTuE01 = gson.fromJson(json, B_InTuE01.class);
            return (B_InTuE01) UserModel.loginStatusCheck(chargeToTuE01);
        }

    }

    //转入余额宝（2）
    public abstract static class GetInTuE2 extends HttpRequest<Been> {

        public void startRequest (String token, String orderId) {
            Parameter parameter = new Parameter();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/transferinB?source=android");
            parameter.methodPost();
            parameter.addValue("token", token);
            parameter.addValue("orderId", orderId);
            startRequest(parameter);
        }

        @Override
        public Been decodeResult (String json) throws Exception {
            Been been = gson.fromJson(json, Been.class);
            return UserModel.loginStatusCheck(been);
        }

    }


    //转出余额宝(1)
    public abstract static class GetOutYuE extends HttpRequest<B_OutYuE1> {

        public void startRequest (String token, int gameId, int money, String fundPwd) {
            Parameter parameter = new Parameter();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/transferoutB?source=android");
            parameter.methodPost();
            parameter.addValue("token", token);
            parameter.addValue("gameId", gameId);
            parameter.addValue("money", money);
            parameter.addValue("fundPwd", fundPwd);
            startRequest(parameter);
        }

        @Override
        public B_OutYuE1 decodeResult (String json) throws Exception {
            B_OutYuE1 outYuE1 = gson.fromJson(json, B_OutYuE1.class);
            return (B_OutYuE1) UserModel.loginStatusCheck(outYuE1);
        }

    }

    //转出余额宝(2)
    public abstract static class GetOutYuE2 extends HttpRequest<Been> {

        public void startRequest (String token, int gameId, String orderId) {
            Parameter parameter = new Parameter();
            parameter.methodPost();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/transferoutP?source=android");
            parameter.addValue("token", token);
            parameter.addValue("gameId", gameId);
            parameter.addValue("orderId", orderId);
            startRequest(parameter);
        }

        @Override
        public Been decodeResult (String json) throws Exception {
            Been been = gson.fromJson(json, Been.class);
            return UserModel.loginStatusCheck(been);
        }

    }


    //余额宝明细
    public abstract static class GetYuEDetail extends HttpRequest<B_YuEDetail> {

        private boolean isRequest = false;
        private int page;

        public void startRequest (@NonNull String token,
                                  @NonNull String startTime, @NonNull String endTime,
                                  int page, int rows,
                                  @Nullable String orderId, int type) {
            if (isRequest) return;
            isRequest = true;
            this.page = page;

            Parameter parameter = new Parameter();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/funddetail?source=android");
            parameter.methodPost();
            parameter.addValue("token", token);
            parameter.addValue("startTime", startTime);
            parameter.addValue("endTime", endTime);
            if (page != 0)
                parameter.addValue("page", page);
            if (rows != 0)
                parameter.addValue("rows", rows);
            if (orderId != null)
                parameter.addValue("orderId", orderId);
            if (type != 0)
                parameter.addValue("type", type);

            startRequest(parameter);
        }

        @Override
        public B_YuEDetail decodeResult (String json) throws Exception {
            isRequest = false;
            B_YuEDetail yuEDetail = gson.fromJson(json, B_YuEDetail.class);
            yuEDetail.setPage(page);
            return (B_YuEDetail) UserModel.loginStatusCheck(yuEDetail);
        }
    }

    //收益明细查询
    public abstract static class GetYuEEconomy extends HttpRequest<B_EconomyCount> {

        private int page;

        public void startRequest (String token, String startTime, String endTime, int page, int rows) {
            this.page = page;

            Parameter parameter = new Parameter();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/incomeinquiry?source=android");
            parameter.methodPost();
            parameter.addValue("token", token);
            parameter.addValue("startTime", startTime);
            parameter.addValue("endTime", endTime);
            if (page != 0)
                parameter.addValue("page", page);
            if (rows != 0)
                parameter.addValue("rows", rows);

            startRequest(parameter);
        }

        @Override
        public B_EconomyCount decodeResult (String json) throws Exception {
            B_EconomyCount economyCount = gson.fromJson(json, B_EconomyCount.class);
            economyCount.setPage(page);
            return (B_EconomyCount) UserModel.loginStatusCheck(economyCount);
        }
    }

    //余额收益比率
    public abstract static class GetYuEEconomyRate extends HttpRequest<B_RateList> {

        public void startRequest (String token) {
            Parameter parameter = new Parameter();
            parameter.methodPost();
            parameter.setUrl(Constants.adress + "/api/m/userbalance/incomeratio?source=android");
            parameter.addValue("token", token);
            startRequest(parameter);
        }

        @Override
        public B_RateList decodeResult (String json) throws Exception {
            B_RateList rateList = gson.fromJson(json, B_RateList.class);
            return (B_RateList) UserModel.loginStatusCheck(rateList);
        }
    }


}
